package com.tadpole.music.presenter.me;

import com.tadpole.music.callback.BaseCallback;
import com.tadpole.music.config.Constant;
import com.tadpole.music.config.UrlConfig;
import com.tadpole.music.iView.login.RegisterIView;
import com.tadpole.music.model.base.DataModel;
import com.tadpole.music.model.base.ModelToken;
import com.tadpole.music.presenter.base.BasePresenter;
import com.tadpole.music.utils.SpUtil;
import com.tadpole.music.utils.ToastUtils;

import org.json.JSONException;
import org.json.JSONObject;

/**
 * 退出登录
 * Created By 孙岩
 * on 2020/9/17
 */
public class LoginOutPresenter extends BasePresenter<RegisterIView> {
    @SuppressWarnings("unchecked")
    public void loginOut() {
        String token = SpUtil.getInstance(getView().getContext()).getString(Constant.TOKEN, "");
        String url = UrlConfig.loginOutV2;
        DataModel.request(ModelToken.POST_HEAD_REQUEST_NO)
                .url(url)
                .token(token)
                .execute(new BaseCallback<String>() {
                    @Override
                    public void onSuccess(String data) {
                        try {
                            JSONObject obj = new JSONObject(data);
                            if (obj.getInt("code") == 200) {
                                getView().showResult();
                            } else if(obj.getInt("code")==401){
                                getView().show401();
                            }else {
                                ToastUtils.show(obj.getString("msg"));
                            }
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    }

                    @Override
                    public void onFailure(String msg) {
                        getView().showErr();
                    }

                    @Override
                    public void onComplete() {
                        getView().hideLoading();
                    }

                    @Override
                    public void onBefore() {
                        getView().showLoading();
                    }
                });
    }
}
